// Copyright (C) 2022 Linlexiao
#include "pointreferencewidget.h"

#include <QDebug>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QSizePolicy>

#include "logic/calculate.h"

PointReferenceWidget::PointReferenceWidget(QWidget* parent)
    : QTabWidget(parent) {
    setWindowTitle("点数速查表");
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
    setWindowIcon(QIcon(":/src/img/icon.png"));
    oya = new QTableWidget(9, 11, this);
    ko = new QTableWidget(9, 11, this);
    _processTable(oya, true);
    _processTable(ko, false);
    addTab(oya, "庄家点数");
    addTab(ko, "子家点数");
}

PointReferenceWidget::~PointReferenceWidget() = default;

void PointReferenceWidget::_processTable(QTableWidget* tbl, bool isOya) {
    QStringList horizontalHeader;
    horizontalHeader << "20符"
                     << "25符"
                     << "30符"
                     << "40符"
                     << "50符"
                     << "60符"
                     << "70符"
                     << "80符"
                     << "90符"
                     << "100符"
                     << "110符";
    tbl->setHorizontalHeaderLabels(horizontalHeader);
    QStringList verticalHeader;
    verticalHeader << "1番"
                   << "2番"
                   << "3番"
                   << "4番"
                   << "5番"
                   << "6~7番"
                   << "8~10番"
                   << "11~12番"
                   << "13番以上";
    tbl->setVerticalHeaderLabels(verticalHeader);
    tbl->setStyleSheet("*{font: 11pt 微软雅黑}");
    tbl->setSelectionMode(QAbstractItemView::NoSelection);
    tbl->setEditTriggers(QAbstractItemView::NoEditTriggers);

    constexpr int MARGIN = 0;
    //普通
    {
        QVector<QVector<QString> > itemStrings;

        if (isOya) {
            itemStrings = {
                {"-", "-", "<b>1500</b><br>(500)", "<b>2000</b><br>(700)",
                 "<b>2400</b><br>(800)", "<b>2900</b><br>(1000)",
                 "<b>3400</b><br>(1200)", "<b>3900</b><br>(1300)",
                 "<b>4400</b><br>(1500)", "<b>4800</b><br>(1600)",
                 "<b>5300</b><br>(-)"},
                {"-<br>(700)", "<b>2400</b><br>(-)", "<b>2900</b><br>(1000)",
                 "<b>3900</b><br>(1300)", "<b>4800</b><br>(1600)",
                 "<b>5800</b><br>(2000)", "<b>6800</b><br>(2300)",
                 "<b>7700</b><br>(2600)", "<b>8700</b><br>(2900)",
                 "<b>9600</b><br>(3200)", "<b>10600</b><br>(3600)"},
                {"-<br>(1300)", "<b>4800</b><br>(1600)",
                 "<b>5800</b><br>(2000)", "<b>7700</b><br>(2600)",
                 "<b>9600</b><br>(3200)", "<b>11600</b><br>(3900)"},
                {"-<br>(2600)", "<b>9600</b><br>(3200)",
                 "<b>11600</b><br>(3900)"},
            };
        } else {
            itemStrings = {
                {"-", "-", "<b>1000</b><br>(300,<br>500)",
                 "<b>1300</b><br>(400,<br>700)", "<b>1600</b><br>(400,<br>800)",
                 "<b>2000</b><br>(500,<br>1000)",
                 "<b>2300</b><br>(600,<br>1200)",
                 "<b>2600</b><br>(700,<br>1300)",
                 "<b>2900</b><br>(800,<br>1500)",
                 "<b>3200</b><br>(800,<br>1600)", "<b>3600</b><br>(-)"},
                {"-<br>(400,<br>700)", "<b>1600</b><br>(-)",
                 "<b>2000</b><br>(500,<br>1000)",
                 "<b>2600</b><br>(700,<br>1300)",
                 "<b>3200</b><br>(800,<br>1600)",
                 "<b>3900</b><br>(1000,<br>2000)",
                 "<b>4500</b><br>(1200,<br>2300)",
                 "<b>5200</b><br>(1300,<br>2600)",
                 "<b>5800</b><br>(1500,<br>2900)",
                 "<b>6400</b><br>(1600,<br>3200)",
                 "<b>7100</b><br>(1800,<br>3600)"},
                {"-<br>(700,<br>1300)", "<b>3200</b><br>(800,<br>1600)",
                 "<b>3900</b><br>(1000,<br>2000)",
                 "<b>5200</b><br>(1300,<br>2600)",
                 "<b>6400</b><br>(1600,<br>3200)",
                 "<b>7700</b><br>(2000,<br>3900)"},
                {"-<br>(1300,<br>2600)", "<b>6400</b><br>(1600,<br>3200)",
                 "<b>7700</b><br>(2000,<br>3900)"},
            };
        }
        int rcnt = 0;
        int ccnt = 0;
        for (const auto& p : itemStrings) {
            ccnt = 0;
            for (const auto& pp : p) {
                //这里为了居中
                QLabel* lb = new QLabel(pp);
                lb->setAlignment(Qt::AlignCenter);
                QWidget* widget = new QWidget(this);
                auto* layout = new QHBoxLayout;
                layout->addWidget(lb);
                layout->setMargin(MARGIN);
                layout->setAlignment(lb, Qt::AlignCenter);
                widget->setLayout(layout);
                tbl->setCellWidget(rcnt, ccnt, widget);
                ccnt++;
            }
            rcnt++;
        }
    }
    //固定
    {
        QVector<QString> strings;
        if (isOya) {
            strings = {"<b>满贯12000</b>(4000)", "<b>跳满18000</b>(6000)",
                       "<b>倍满24000</b>(8000)", "<b>三倍满36000</b>(12000)",
                       "<b>累计役满48000</b>(16000)"};
        } else {
            strings = {
                "<b>满贯8000</b>(2000,4000)", "<b>跳满12000</b>(3000,6000)",
                "<b>倍满16000</b>(4000,8000)", "<b>三倍满24000</b>(6000,12000)",
                "<b>累计役满32000</b>(8000,16000)"};
        }

        tbl->setSpan(2, 6, 1, 5);
        tbl->setSpan(3, 3, 1, 8);
        tbl->setSpan(4, 0, 1, 11);
        tbl->setSpan(5, 0, 1, 11);
        tbl->setSpan(6, 0, 1, 11);
        tbl->setSpan(7, 0, 1, 11);
        tbl->setSpan(8, 0, 1, 11);
        for (int i = 0; i < 5; i++) {
            if (i == 0) {
                for (int j = 2; j < 5; j++) {
                    QLabel* lb = new QLabel(strings[i]);
                    lb->setAlignment(Qt::AlignCenter);
                    QWidget* widget = new QWidget(this);
                    auto* layout = new QHBoxLayout;
                    layout->addWidget(lb);
                    layout->setMargin(MARGIN);
                    layout->setAlignment(lb, Qt::AlignCenter);
                    widget->setLayout(layout);
                    tbl->setCellWidget(j, 10, widget);
                }
            } else {
                QLabel* lb = new QLabel(strings[i]);
                lb->setAlignment(Qt::AlignCenter);
                QWidget* widget = new QWidget(this);
                auto* layout = new QHBoxLayout;
                layout->addWidget(lb);
                layout->setMargin(MARGIN);
                layout->setAlignment(lb, Qt::AlignCenter);
                widget->setLayout(layout);
                tbl->setCellWidget(i + 4, 10, widget);
            }
        }
    }

    tbl->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tbl->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
    tbl->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tbl->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    tbl->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
}

QSize PointReferenceWidget::sizeHint() const { return {620, 400}; }
